package com.imay.photopicker.adapter;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.imay.capturefilter.R;
import com.imay.photopicker.entity.Photo;
import com.imay.photopicker.util.AndroidLifecycleUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/16.
 */

public class PhotoGridAdapter extends BaseMultiItemQuickAdapter<Photo, BaseViewHolder> {


    private final ArrayList<Object> selectedPhotos;

    private int columnNumber;
    private int imageSize;

    public static final int FIRST_ITEM = 1;
    public static final int OTHER_ITEM = 2;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     */
    public PhotoGridAdapter(Context context, List<Photo> data, ArrayList<String> orginalPhotos, int columnNumber) {
        super(data);
        addItemType(FIRST_ITEM, R.layout.picker_item_photo_picker_carema);
        addItemType(OTHER_ITEM, R.layout.picker_item_photo_picker_img);

        setColumnNumber(context, columnNumber);
        selectedPhotos = new ArrayList<>();
        if (orginalPhotos != null) selectedPhotos.addAll(orginalPhotos);
    }

    private void setColumnNumber(Context context, int columnNumber) {
        this.columnNumber = columnNumber;
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);
        int widthPixels = metrics.widthPixels;
        // 减去分割线
        // widthPixels -= (columnNumber-1)*context.getResources().getDimension(R.dimen.picker_item_photo_rule);
        imageSize = widthPixels / columnNumber;
    }

    @Override
    protected void convert(BaseViewHolder helper, Photo item) {
        if (item.getItemType() == FIRST_ITEM) {


        } else if (item.getItemType() == OTHER_ITEM) {
            ImageView ivPhoto = helper.getView(R.id.item_iv_photo);
            ImageView ivSelect = helper.getView(R.id.item_iv_selecter);
            boolean canLoadImage = AndroidLifecycleUtils.canLoadImage(ivPhoto.getContext());

            if (canLoadImage) {
                RequestOptions options = new RequestOptions()
                        .centerCrop().override(imageSize, imageSize)
                        .placeholder(R.drawable.placeholder)
                        .error(R.drawable.placeholder)
                        .priority(Priority.NORMAL).dontAnimate();
                Glide.with(mContext).load(item.getPath()).apply(options).into(ivPhoto);
            }
            if(item.isSelect()){
                ivSelect.setSelected(true);
            }else{
                ivSelect.setSelected(false);
            }
        }
    }
}
